import updatePay from '../update-pay/update-pay.vue'
import Pages from '../../pages/pages.vue'
import addPay from '../add-pay/add-pay.vue'
import swapPosition from '../swap-position/swap-position.vue'
import { showMsg, getPayPortfolioList, deletePayPortfolio, swapGolds, doRecommend, doUnrecommend, setMenuType } from '../../../vuex/action'

export default {
  components: {
    'vue-modify-pay': updatePay,
    'vue-pages': Pages,
    'vue-add-pay': addPay,
    'vue-swap-position': swapPosition
  },
  data() {
    return {
      payList: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      }
    }
  },
  ready() {
    this.xSetMenuType('portfolio')
    this.searchPay(0)
  },
  vuex: {
    getters: {
      fresh: state => state.ptListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetlist: getPayPortfolioList,
      xDelete: deletePayPortfolio,
      xSwapGolds: swapGolds,
      xRecommend: doRecommend,
      xUnrecommend: doUnrecommend,
      xSetMenuType: setMenuType
    }
  },
  methods: {
    searchPay({start=this.pages.start, limit=this.pages.limit}) {
      this.xGetlist({
        start: start,
        limit: limit
      })
        .then((res) => {
          this.payList = res.data.data.list
          this.pages.totalPages = Math.ceil(res.data.data.totalCount / this.pages.limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    deleteAc(list) {
      var formData = new FormData()
      list.betaId = +list.betaId
      for (let name in list) {
        if (name === 'betaId') {
          formData.append(name, list[name])
        }
      }
      this.xDelete(formData)
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    moveUp(index) {
      if (index === 0) {
        return this.xShowMsg({
          type: 'danger',
          content: '已经是第一位'
        })
      }
      let fromBetaId = this.payList[index].betaId
      let toBetaId = this.payList[index - 1].betaId
      this.swapPosition(fromBetaId, toBetaId)
    },
    moveDown(index) {
      if (index === this.payList.length - 1) {
        return this.xShowMsg({
          type: 'danger',
          content: '已经是最后一位'
        })
      }
      let fromBetaId = this.payList[index].betaId
      let toBetaId = this.payList[index + 1].betaId
      this.swapPosition(fromBetaId, toBetaId)
    },
    swapPosition(fromId, toId) {
      var formData = new FormData()
      formData.append('fromBetaId', fromId)
      formData.append('toBetaId', toId)
      this.xSwapGolds(formData)
        .then(() => {
          this.searchPay({})
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    querySubs(item) {},
    recommend(item) {
      this.xRecommend({
        betaId: item.betaId
      })
        .then(() => {
          item.priority = 1
          this.xShowMsg({
            type: 'success',
            content: '优先推荐成功'
          })
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    unrecommend(item) {
      this.xUnrecommend({
        betaId: item.betaId
      })
        .then(() => {
          item.priority = 0
          this.xShowMsg({
            type: 'success',
            content: '取消推荐成功'
          })
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.searchPay({})
      }
    }
  }
}
